package yt.kpool.activity.personal;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.activity.setting.EditAccountActivity;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.BaseBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.SPUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.dialog.SuccessDialog;

/**
 * 修改密码第二部
 */
public class ForgetPwd2Activity extends AppCompatActivity {

    @BindView(R.id.edit_pwd)
    EditText edit_pwd;
    @BindView(R.id.tv_pwd2)
    EditText tv_pwd2;
    private LoadingDialog loadingDialog;
    public static void start(Activity mactivity, String Phone, String SmsCode) {
        Intent intent = new Intent(mactivity, ForgetPwd2Activity.class);
        intent.putExtra("Phone", Phone);
        intent.putExtra("SmsCode", SmsCode);

        mactivity.startActivity(intent);
    }

    public String getPhone() {
        return getIntent().getStringExtra("Phone");
    }
    public String getSmsCode() {
        return getIntent().getStringExtra("SmsCode");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd2);
        ButterKnife.bind(this);
        initTitle();
    }
    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("找回密码");
        loadingDialog=new LoadingDialog(this,0);
    }
    @OnClick({R.id.btn_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_next://确认
                if (check()) {
                    retrievePassWord();
                }

                break;

        }
    }

    private void retrievePassWord() {
        if (NetWorkUtil.check(this)) {

            loadingDialog.show();
            NetHelperNew.RetrievePassWord(getPhone(),getSmsCode(),getPwd(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                    Log.i("-->", "忘记密码:" + baseBean.getContent());
                    if (baseBean.getType() == 1) {

                       new  SuccessDialog(ForgetPwd2Activity.this, "密码设置成功", new SuccessDialog.OnSuccessListener() {
                           @Override
                           public void getResult() {
                               finish();
                           }
                       }).show();
                    } else {
                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }

    }

    private String getPwd() {
        return edit_pwd.getText().toString().trim();
    }

    private String getPwd2() {
        return tv_pwd2.getText().toString().trim();
    }



    private boolean check() {
        if (getPwd().length() == 0) {
            ToastUtil.showToast(getApplicationContext(), "请输入新密码");
            return false;
        }
        if (getPwd2().length() == 0) {
            ToastUtil.showToast(getApplicationContext(), "请输入确认密码");
            return false;
        }
        if (!getPwd().equals(getPwd2())) {
            ToastUtil.showToast(getApplicationContext(), "两次密码不一致");
            return false;
        }
        return true;
    }
}
